package wang.z.component;

import wang.z.JFrame.loginFrm;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.ResultInfoData2Vector;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import static javax.swing.JTable.AUTO_RESIZE_OFF;

/**
 * @author like
 * @date 2022/12/13 17:15
 * @Description TODO
 */
public class BookTypeUpdate {
    private JTextField textField_1;//编号
    private JTextField textField_2;//类别名称
    private JTextField textField_3;//描述
    private Vector<Vector> Date;//标题
    private DefaultTableModel Model;
    private JPanel panel_1 = new JPanel();
    private JPanel panel_2 = new JPanel();
     private JTable Table;
     private JFrame jf = null;
     private  String typeId="";
    public BookTypeUpdate(JFrame jf){
        this.jf = jf;
        panel_1.setBorder(new TitledBorder(null, "类别信息", TitledBorder.LEADING, TitledBorder.TOP,
                null, new Color(255, 0, 0)));
        panel_1.setBounds(20, 20, 541, 260);
        String[] title = {"编号","类别名称","类别描述"};
        Vector BookTitle = new Vector();
        for (String i : title) {
            BookTitle.add(i);
        }
        /*具体的各栏行记录 先用空的二维数组占位*/
        Date = new Vector<Vector>();

        Model = new DefaultTableModel(Date, BookTitle);
        Table = new JTable(Model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        Table.getSelectionModel().setSelectionMode(AUTO_RESIZE_OFF);
        Table.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int selectedRow = Table.getSelectedRow();
                String id = Model.getValueAt(selectedRow, 0).toString();
                String typesName = Model.getValueAt(selectedRow, 1).toString();
                String types = Model.getValueAt(selectedRow, 2).toString();
                textField_1.setText(id);
                textField_2.setText(typesName);
                textField_3.setText(types);
                typeId = id;
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        getDate();
        panel_1.setLayout(null);
        JScrollPane jscrollpane = new JScrollPane();
        jscrollpane.setBounds(20, 20, 496, 230);
        jscrollpane.setViewportView(Table);
        panel_1.add(jscrollpane);

        panel_2.setBorder(new TitledBorder(null, "类别编辑", TitledBorder.LEADING, TitledBorder.TOP,
                null, new Color(255, 0, 0)));
        panel_2.setBounds(20, 280, 541, 160);
        panel_2.setLayout(null);
        JLabel label = new JLabel("编号：");
        label.setFont(new Font("幼圆", Font.BOLD, 14));
        label.setBounds(58, 20, 80, 27);
        panel_2.add(label);
        textField_1 = new JTextField();
        textField_1.setColumns(20);
        textField_1.setBounds(101, 20, 100, 27);
        panel_2.add(textField_1);
        JLabel label_1 = new JLabel("类别名称：");
        label_1.setFont(new Font("幼圆", Font.BOLD, 14));
        label_1.setBounds(280, 20, 80, 27);
        panel_2.add(label_1);
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(350, 20, 100, 27);
        panel_2.add(textField_2);
        JLabel label_2 = new JLabel("描述：");
        label_2.setFont(new Font("幼圆", Font.BOLD, 14));
        label_2.setBounds(58, 68, 45, 27);
        panel_2.add(label_2);
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(101, 68, 350, 27);
        panel_2.add(textField_3);

        JButton btnNewButton_1 = new JButton("修改");
        btnNewButton_1.setBounds(111,108,100,27);
        btnNewButton_1.setFont(new Font("幼圆", Font.BOLD, 14));
        btnNewButton_1.setBackground(new Color(250,120,0));
        panel_2.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("删除");
        btnNewButton_2.setBounds(290,108,100,27);
        btnNewButton_2.setFont(new Font("幼圆", Font.BOLD, 14));
        panel_2.add(btnNewButton_1);
        panel_2.add(btnNewButton_2);

        btnNewButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = textField_1.getText().trim();
                String typeName = textField_2.getText().trim();
                String remark = textField_3.getText().trim();
                Map<String,String> params = new HashMap<>();
                params.put("id",id);
                params.put("typeName",typeName);
                params.put("remark",remark);
                params.put("typeId",typeId);
                PostUtils.postWithParams("http://localhost:8080//book_type/Update",params,new SuccessListener() {
                            //请求发送成功，服务器正常响应，会执行这里
                            @Override
                            public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                                ResultInfo info = JsonUtils.parseResult(result);
                                getDate();
                                //判断info中的flag标记
                                if (info.isFlag()) {

                                } else {
                                    JOptionPane.showMessageDialog(jf, info.getMessage());
                                    if(info.getMessage().equals("用户尚未登录")){
                                        new loginFrm().init();
                                        jf.dispose();
                                    }
                                }
                            }
                        }
                        , new FailListener() {
                            //当该请求发送失败后会执行这个方法
                            @Override
                            public void fail() {
                                JOptionPane.showMessageDialog(jf, "传入的数据有误或者网路问题，请检查");
                            }
                        }
                );
            }
        });
        btnNewButton_2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Map<String,String> params = new HashMap<>();
                params.put("TypeId",typeId);
                PostUtils.postWithParams("http://localhost:8080//book_type/remove",params,new SuccessListener() {
                            //请求发送成功，服务器正常响应，会执行这里
                            @Override
                            public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                                ResultInfo info = JsonUtils.parseResult(result);
                                JOptionPane.showMessageDialog(jf, info.getMessage());
                                getDate();
                                //判断info中的flag标记
                                if (info.isFlag()) {

                                } else {
                                    JOptionPane.showMessageDialog(jf, info.getMessage());
                                    if(info.getMessage().equals("用户尚未登录")){
                                        new loginFrm().init();
                                        jf.dispose();
                                    }
                                }
                            }
                        }
                        , new FailListener() {
                            //当该请求发送失败后会执行这个方法
                            @Override
                            public void fail() {
                                JOptionPane.showMessageDialog(jf, "传入的数据有误或者网路问题，请检查");
                            }
                        }
                );
            }
        });

        jf.add(panel_1);
        jf.add(panel_2);
        jf.add(new JPanel());
    }
    public void getDate(){

        PostUtils.post("http://localhost:8080//book_type/getAll",new SuccessListener() {
                    //请求发送成功，服务器正常响应，会执行这里
                    @Override
                    public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                        ResultInfo info = JsonUtils.parseResult(result);
                        Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);

                        Date.clear();
                        for (Vector vector : vectors) {
                            Date.add(vector);
                        }
                        Model.fireTableDataChanged();
                        //判断info中的flag标记
                        if (info.isFlag()) {

                        } else {
                            JOptionPane.showMessageDialog(jf, info.getMessage());
                            if(info.getMessage().equals("用户尚未登录")){
                                new loginFrm().init();
                                jf.dispose();
                            }
                        }
                    }
                }
                , new FailListener() {
                    //当该请求发送失败后会执行这个方法
                    @Override
                    public void fail() {
                        JOptionPane.showMessageDialog(jf, "传入的数据有误或者网路问题，请检查");
                    }
                }
        );
    }

    }

